"""
YouTour - Create and access tours based on public transport routes. 
Copyright (C) 2011 Karl Beecher

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from data.Models import TourDB, StopDB
import datetime as dt
from google.appengine.ext import db
import logging


class DbInterface():

    @staticmethod
    def getTourById(tourId):
        tour = db.get(tourId)

        if tour == None:
            logging.error("Couldn't find selected tour in DB!")

        return tour

    @staticmethod
    def getAllTours():
        tourQ = TourDB.all()

        return tourQ



    @staticmethod
    def storeTour(tour):
        tour.put()


    @staticmethod
    def storeStop(stop):
        stop.put()


    @staticmethod
    def getStopById(stopId):
        return db.get(stopId)


    @staticmethod
    def addAudioFile(stopId, blobKey):
        stop = DbInterface.getStopById(stopId)
        stop.audioFile = blobKey
        stop.put()


    @staticmethod
    def testPop(audioFile):
        # Temp db population

        allStops = StopDB.all()
        logging.info("Deleting stops: " + str(allStops.count()))
        db.delete(allStops)

        allTours = TourDB.all()
        logging.info("Deleting tours: " + str(allTours.count()))
        db.delete(allTours)

        newTour = TourDB(title = "Berlin Mitte",
                       description = "A tour of the central area of Berlin",
                       startPosition = "abc",
                       endPosition = "xyz",
                       startTime = dt.time(10, 0),
                       endTime = dt.time(12, 0))
        newTour2 = TourDB(title = "Berlin Schoeneberg",
                       description = "A tour of the Schoeneberg area of Berlin",
                       startPosition = "klm",
                       endPosition = "nop",
                       startTime = dt.time(10, 0),
                       endTime = dt.time(12, 0))
        newTour.put()
        newTour2.put()

        newStop = StopDB(stopNumber = 1,
                       tourId = newTour,
                       description = "We start at Brandenburger Tor.",
                       directions = "Walk down Unter den Linden towards the bright light.",
                       audioFile = db.Blob(audioFile))

        newStop2 = StopDB(stopNumber = 2,
                       tourId = newTour,
                       description = "Then we reach the Holocaust memorial",
                       directions = "Head north.")

        newStop.put()
        newStop2.put()
